/******************************************************************************
*
* This file is part of FreeTrack (http://code.google.com/p/qtfreetrack).
* BY Willy Gardiol (willy@gardiol.org)
*
* FreeTrack is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* FreeTrack is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Foobar.  If not, see <http://www.gnu.org/licenses/>
*
******************************************************************************/

#include "freetrackwidget.h"

FreeTrackWidget::FreeTrackWidget(QWidget *parent) :
    QWidget(parent),
    current_orientation(not_oriented)
{
    // configure widget: make sure window is not transparent (bug on symbian) and
    // no contect menu is assigned (another bug on symbian)
    setBackgroundRole( QPalette::Window );
    setContextMenuPolicy(Qt::NoContextMenu);
}

void FreeTrackWidget::resizeEvent(QResizeEvent * ev)
{
    QWidget::resizeEvent(ev);
    checkOrientation();
}

void FreeTrackWidget::showEvent(QShowEvent * ev)
{
    checkOrientation();
    QWidget::showEvent(ev);
}

void FreeTrackWidget::checkOrientation()
{
    // Get current screen geometry and set initial orientation.
    QRect screen_size;
    if ( QApplication::activeWindow() )
        screen_size = QApplication::activeWindow()->geometry();
    else
        screen_size = QApplication::desktop()->screenGeometry();
    if ( screen_size.width() > screen_size.height() )
    {
        if ( current_orientation != horizontal )
            orientateWidget( horizontal );
    }
    else
    {
        if ( current_orientation != vertical )
            orientateWidget( vertical );
    }
}

FreeTrackWidget::~FreeTrackWidget()
{
}

void FreeTrackWidget::orientateWidget(Orientation new_orientation)
{
    QLayout* new_layout = 0;
    if ( new_orientation != current_orientation )
    {
        current_orientation = new_orientation;
        // Get new layout
        switch ( new_orientation)
        {
        case vertical:
            new_layout =  createVertical();
            break;
        case horizontal:
            new_layout = createHorizontal();
            break;
        default:
            new_layout = 0;
        }
        // If we have a new layout...
        if ( new_layout != 0 )
        {   // Delete old layout first (required by Qt)
            if ( layout() != 0 )
                delete layout();
            // then set the new layout.
            setLayout( new_layout );
            new_layout->activate();
            new_layout->update();
        }
    }
}

QLayout* FreeTrackWidget::createHorizontal()
{
    return 0;
}

QLayout* FreeTrackWidget::createVertical()
{
    return 0;
}
